package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //处理查询所有部门的请求 /department/list
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageInfo<Department> pageInfo = departmentService.query(qo);//查出数据
        model.addAttribute("pageInfo",pageInfo);
        //model拿到数据后与jsp合并渲染成HTML格式，展现在页面上
        return "department/list";  // /WEB-INF/views/department/list.ftl
    }


    //处理删除请求 /department/delete?id=1
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null) {
            departmentService.delete(id);
        }
        return "redirect:/department/list";  //localhost/department/list
    }

    @RequiredPermission(name = "部门新增/修改", expression = "department.saveOrUpdate")
    //处理保存或者修改请求请求 /department/saveOrUpdate  //参数在请求体中，操作input界面的提交功能
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
        if (department.getId()==null) {
            departmentService.save(department);
        }else{
            departmentService.update(department);
        }
        return "redirect:/department/list";  //localhost/department/list
    }
}
